package net.NingXiaRuiTai.ShortChain.Manager.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.NingXiaRuiTai.ShortChain.Manager.GroupCodeMappingManager;
import net.NingXiaRuiTai.ShortChain.enums.ShortLinkStateEnum;
import net.NingXiaRuiTai.ShortChain.mapper.GroupCodeMappingMapper;
import net.NingXiaRuiTai.ShortChain.model.GroupCodeMappingDO;
import net.NingXiaRuiTai.ShortChain.model.VO.GroupCodeMappingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class GroupCodeMappingManagerImpl implements GroupCodeMappingManager {
    @Resource
    GroupCodeMappingMapper groupCodeMappingMapper;


    @Override
    public GroupCodeMappingDO findByCodeAndGroupId(String shortLinkCode, Long GroupId, Long accountNo) {
        GroupCodeMappingDO groupCodeMappingDO = groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("code", shortLinkCode).eq("account_no", accountNo).eq("group_id", GroupId));

        return groupCodeMappingDO;
    }

    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long accountNo, Long groupId) {
        GroupCodeMappingDO groupCodeMappingDO = groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("id", mappingId).eq("account_no", accountNo)
                .eq("group_id", groupId));
        return groupCodeMappingDO;
    }

    @Override
    public int add(GroupCodeMappingDO groupCodeMappingDO) {
        int insert = groupCodeMappingMapper.insert(groupCodeMappingDO);
        return insert;
    }

    @Override
    public int del(String shortLinkCode, Long accountNo, Long groupId) {
        int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("code", shortLinkCode)
                .eq("account_no", accountNo)
                .eq("group_id", groupId).set("del", 1));
        return 0;
    }

    @Override
    public int delShortLink(GroupCodeMappingDO groupCodeMappingDO) {
        int rows = groupCodeMappingMapper.update(null,
                new UpdateWrapper<GroupCodeMappingDO>()
                        .eq("code", groupCodeMappingDO.getCode())
                        .eq("group_id", groupCodeMappingDO.getGroupId())
                        .eq("account_no", groupCodeMappingDO.getAccountNo())
                        .set("del", 1));
        return rows;
    }

    @Override
    public Map<String, Object> pageShortLinkByGroupId(Integer page, Integer size, Long accountNo, Long groupId) {
        Page<GroupCodeMappingDO> pageInfo = new Page<>(page, size);
        Page<GroupCodeMappingDO> groupCodeMappingDOPage = groupCodeMappingMapper.selectPage(pageInfo, new QueryWrapper<GroupCodeMappingDO>()
                .eq("account_no", accountNo)
                .eq("group_id", groupId).eq("del", 0));
        HashMap<String, Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", groupCodeMappingDOPage.getTotal());
        pageMap.put("total_page", groupCodeMappingDOPage.getPages());
        pageMap.put("current_data", groupCodeMappingDOPage.getRecords()
                .stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));


        return pageMap;
    }

    private GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO) {
        GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
        BeanUtils.copyProperties(groupCodeMappingDO, groupCodeMappingVO);

        return groupCodeMappingVO;
    }

    @Override
    public int updateGroupCodeMappingState(Long accountNo, Long groupId, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {
        int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("code", shortLinkCode)
                .eq("account_no", accountNo)
                .eq("group_id", groupId)
                .eq("del", 0)
                .set("state", shortLinkStateEnum.name()));

        return rows;
    }

    @Override
    public int updateShortLink(GroupCodeMappingDO groupCodeMappingDO) {


        int rows = groupCodeMappingMapper.update(null,
                new UpdateWrapper<GroupCodeMappingDO>()
                       .eq("code", groupCodeMappingDO.getCode())
                       .eq("del", 0)
                       .eq("account_no", groupCodeMappingDO.getAccountNo())
                        .set("title",groupCodeMappingDO.getTitle())
                        .set("domain",groupCodeMappingDO.getDomain())


        );
        return rows;
    }
}
